package com.example.flashsales.service.impl;

import com.example.flashsales.entity.pojo.OrderDetails;
import com.example.flashsales.entity.pojo.TimeConfig;
import com.example.flashsales.entity.vo.OrderDetailsVo;
import com.example.flashsales.mappers.OrderDetailsMapper;
import com.example.flashsales.service.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Autowired
    TimeServiceImpl timeService;

    @Autowired
    OrderDetailsMapper orderDetailsMapper;

    /**
     * 根据商品id查看为分拣的订单细节
     * @param goodsId
     * @return
     */
    @Override
    public List<OrderDetails> viewDetails(long goodsId,Double count) {
        final TimeConfig timeConfig = timeService.viewTime();
        //TODO 只能查看到前天晚上六点到昨天晚上六点
        final Time time = timeConfig.getTime();
        Date date = new Date();
        Date date1 = new Date();
        //当天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        calendar1.add(Calendar.DATE, -1);
        date1 = calendar1.getTime();
        final String format = new SimpleDateFormat("yyyy-MM-dd").format(date1);
        final String d = format +" "+ time;
        //前一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -2);
        date = calendar.getTime();
        final String format1 = new SimpleDateFormat("yyyy-MM-dd").format(date);
        final String q = format1 +" "+ time;

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date qDate = null;
        Date hDate = null;
        try {
            qDate = f.parse(q);
            hDate = f.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        final List<OrderDetails> orderDetails = orderDetailsMapper.viewDetailsByGoodsId(goodsId,qDate,hDate,count);
        return orderDetails;
    }

    /**
     * 修改订单细节状态
     * @param detailsId
     * @return
     */
    @Override
    public boolean updateDetailsState(long detailsId,long state) {
        return orderDetailsMapper.updateState(detailsId,state) > 0;
    }

    /**
     * 根据细节id获取细节
     * @param detailsId
     * @return
     */
    @Override
    public OrderDetails viewOrderDetails(long detailsId) {
        return orderDetailsMapper.viewOrderDetails(detailsId);
    }
}
